<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,initial-scale=1.0,width=device-width"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>大奔农庄</title>
    <link rel="stylesheet" type="text/css" href="../../css/api.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/aui.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/style.css"/>
    <link rel="stylesheet" href="../../css/swiper.min.css">
    <link rel="stylesheet" href="../../font/iconfont.css">
</head>
<body>
	<div class="aui-content">
	     <ul class="aui-list aui-media-list">
       	    <li class="aui-list-item">
	            <div class="aui-media-list-item-inner">
	                <div class="aui-list-item-inner">
	                    <div class="aui-list-item-text">
	                        <div class="aui-list-item-title">订单号：</div>
	                        <div class="aui-list-item-text aui-font-size-16" id="order_sn"></div>
	                    </div>
	                </div>
	            </div>
       	    </li>
       	    <li class="aui-list-item">
	            <div class="aui-media-list-item-inner">
	                <div class="aui-list-item-inner">
	                    <div class="aui-list-item-text">
	                        <div class="aui-list-item-title">总计：</div>
	                        <div class="aui-list-item-text aui-font-size-16" id="order_amount"></div>
	                    </div>
	                </div>
	            </div>
       	    </li>
   	   </ul>
    </div>
	<div class="aui-content aui-margin-b-15">
	    <ul class="aui-list aui-list-in">
	    	<li class="aui-list-header">
	    		选择支付方式
	    	</li>
		    <li class="aui-list-item">
		        <div class="aui-list-item-inner">
		            <div class="aui-list-item-title">支付宝</div>
		            <div class="aui-list-item-right">
		                <input type="radio" class="aui-radio" name="demo1" checked onclick="change_payment('ALI','ALI_APP');">
		            </div>
		        </div>
		    </li>
		    <li class="aui-list-item">
		        <div class="aui-list-item-inner">
		            <div class="aui-list-item-title">微信</div>
		            <div class="aui-list-item-right">
		                <input type="radio" class="aui-radio" name="demo1" onclick="change_payment('WX','WX_APP');">
		            </div>
		        </div>
		    </li>
	    </ul>
    </div>
    <div class="aui-content-padded">
		 <p><div class="aui-btn aui-btn-block aui-btn-success aui-btn-sm" onclick="doPay();">确认支付</div></p>
	 </div>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/common.js"></script>
<script>
var db_key = 'user',db_user, detailParam={},myParam,payment='ALI',paychannel='ALI_APP',ordertitle='支付订单';
apiready = function(){
	myParam = api.pageParam;//获取参数
    getuser();
    domfresh();//更新待支付订单的显示数据
    api.parseTapmode();
}
//获取用户储存在本地的数据
function getuser() {
    db_user = $api.getStorage(db_key);
}
//从服务获取并展示 用户账户数据
function domfresh() {
    detailParam.rtk = RSapi.myrtk;
    detailParam.mytoken = db_user.mytoken;
    detailParam.oid = myParam.oid;

    api.ajax({
        url: RSapi.base+RSapi.OrderInfo,
        method: 'post',
        cache: false,
        timeout: 30,
        dataType: 'json',
        data: { values: detailParam },
    }, function(ret, err){
        if (err) {
            api.alert({msg:err.msg});
        }else if (ret.status) {
            $api.html($api.byId("order_amount"), '￥'+ret.orderinfo.zongjia);
            $api.html($api.byId("order_sn"), ret.orderinfo.order_sn);
        }else {
            api.toast({msg:ret.info});
        }
    });
    api.parseTapmode();
}
//支付操作
function doPay() {
    api.showProgress();
    var useParam = {
        rtk : RSapi.myrtk,
        mytoken : db_user.mytoken,
        payment : payment,
        oid : myParam.oid,
    };

    api.ajax({
        url: RSapi.base+RSapi.orderDoPay,
        method: 'post',
        cache: false,
        timeout: 30,
        dataType: 'json',
        data: { values: useParam },
    }, function(ret, err){
        if (err) {
            api.hideProgress();
            api.alert({msg:err.msg});
        }else if (ret.status) {
            //成功 ret.paybills 对象
            var payData = {
                channel : paychannel,
		        title: ret.paybills.title,
//		        totalfee: ret.paybills.amount*100,
            totalfee: ret.paybills.amount_real,
		        billno: ret.paybills.out_trade_no,
		        optional: {'paybillid': ret.paybills.pay_id}
            }
//          alert(JSON.stringify(payData));
//			api.alert({msg:payData});
            beecloud(payData);
        }else {
            api.hideProgress();
            api.toast({msg:ret.info});
        }
    });
}

function beecloud(payData) {
//	  var payData = {
//      channel: "UN_APP",
//      title: "apicloud",
//      totalfee: 1,
//      billno: "201508191436987",
//      optional: { 'userID': '张三', 'mobile': '0512-86861620' }
//  };

    api.showProgress();
    var beecloud = api.require('beecloud');

    beecloud.pay(payData, function(ret, err){
        api.hideProgress();
        api.alert({ msg: ret.result_msg });
//      alert(JSON.stringify(ret));
//      alert(JSON.stringify(err));
        //支付成功
        if (ret.result_code == 0){
			//发送事件更新订单列表
	    	api.sendEvent({
                name: 'order_list_change',
                extra: {}
            });
		    //打开订单详情页面
		    openOrderInfo(myParam.oid);
            //关闭当前页面 (支付页面)
            api.closeWin();
        }
    });
}
//打开订单详情
function openOrderInfo(id){
	api.openWin({
        name: 'win_orderinfo',
        url: '../win/orderinfo_win.html',
        pageParam: {oid: id}
    });
}
//选择支付方式
function change_payment(payment_type,paychannel_type){
	payment = payment_type;
	paychannel = paychannel_type;
}
</script>
</body>
</html>
